/*
 * wh2_syscalls.h
 *
 *  Created on: 25/04/2012
 *      Author: Roy
 */

#ifndef WH2_SYSCALLS_H_
#define WH2_SYSCALLS_H_

#define MODE_REGULAR 301
#define MODE_RANDOM 302
#include <errno.h>
struct random_params{
	int p_prio;
	int p_epoch;
	int p_interactive;
};

struct switch_info{
	int cur_mechanism;
	int previous_pid;
	int next_pid;
	int previous_priority;
	int next_priority;
	int best_priority;
	int is_new_epoch;
	unsigned long time;
	int reason;
};

enum {TASK_FORKED = 1, TASK_EXIT, TASK_YIELD,
	  TASK_WAITING, TASK_WAKEUP, TASK_TIME_ENDED, TASK_DEFAULT };


int sched_setmechanism(int mode, struct random_params* params){
	long __res;
	__asm__ volatile (
		"movl $243, %%eax;"
		"movl %1, %%ebx;"
		"movl %2, %%ecx;"
		"int $0x80;"
		"movl %%eax,%0"
		: "=m" (__res)
		: "m" (mode), "m"((long)params)
		: "%eax", "%ebx", "%ecx"
	);
	if ((unsigned long)(__res) >= (unsigned long)(-125)) {
		errno = -(__res);
		__res = -1;
	}
	return (int)(__res);
}

int sched_getmechanism(struct random_params* params){
	long __res;
	__asm__ volatile (
		"movl $244, %%eax;"
		"movl %1, %%ebx;"
		"int $0x80;"
		"movl %%eax,%0"
		: "=m" (__res)
		: "m" ((long)params)
		: "%eax","%ebx"
	);
	if ((unsigned long)(__res) >= (unsigned long)(-125)) {
		errno = -(__res);
		__res = -1;
	}
	return (int)(__res);
}

int is_interactive(int pid){
	long __res;
	__asm__ volatile (
		"movl $245, %%eax;"
		"movl %1, %%ebx;"
		"int $0x80;"
		"movl %%eax,%0"
		: "=m" (__res)
		: "m" (pid)
		: "%eax","%ebx"
	);
	if ((unsigned long)(__res) >= (unsigned long)(-125)) {
		errno = -(__res);
		__res = -1;
	}
	return (int)(__res);
}

int get_cur_running_priority(int* best_ready_priority){
	long __res;
	__asm__ volatile (
		"movl $246, %%eax;"
		"movl %1, %%ebx;"
		"int $0x80;"
		"movl %%eax,%0"
		: "=m" (__res)
		: "m" ((long)best_ready_priority)
		: "%eax","%ebx"
	);
	if ((unsigned long)(__res) >= (unsigned long)(-125)) {
		errno = -(__res);
		__res = -1;
	}
	return (int)(__res);
}

int get_scheduling_statistic(struct switch_info* result){
	long __res;
	__asm__ volatile (
		"movl $247, %%eax;"
		"movl %1, %%ebx;"
		"int $0x80;"
		"movl %%eax,%0"
		: "=m" (__res)
		: "m" ((long)result)
		: "%eax","%ebx"
	);
	if ((unsigned long)(__res) >= (unsigned long)(-125)) {
		errno = -(__res);
		__res = -1;
	}
	return (int)(__res);
}

#endif /* WH2_SYSCALLS_H_ */
